from PySide2.QtGui import *
from PySide2.QtWidgets import *

from api.api_view import update_tb_contract
from common.config import Contract_Excel_path

from api.api_project import Public_Data, get_contract_df
from common.pyside2_table.qtable import Base_Table

from common.pyside2_table.qt_wgt import Th_Base, show_msg, open_path
from view.common_fun import get_public_contract_info
from view.table_col_list import contract_col_model_list
from view.th_view import Th_Update_file
from project.web_comm import get_token


class View_Contract(QWidget):
    def __init__(self):
        super().__init__()
        self.update_file_thread = None
        self.view_name = "tab_contract"
        self.data = Public_Data()
        self.setupUi()
        self.bind()
        get_public_contract_info()

    def setupUi(self):
        """设置合同页面控件"""
        # region 布局
        # 整体布局为垂直布局
        lt_pg = QVBoxLayout(self)
        fm_ctrl = QFrame()  # 操作栏
        lt_pg.addWidget(fm_ctrl)
        lt_header = QHBoxLayout(fm_ctrl)

        # endregion

        # region 操作栏
        # 左侧
        self.btn_open_contract = QPushButton("打开合同表")
        self.btn_update_contract = QPushButton("合同更新")

        # 中间，合同选项
        # 项目cmb

        self.ck_has_delivery = QCheckBox(text="隐藏完工合同")

        left_spacer = QSpacerItem(120, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        # right_spacer = QSpacerItem(50, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.btn_reset_contract_info = QPushButton("重置合同信息")
        self.btn_save_contract_info = QPushButton("保存合同更改")
        end_spacer = QSpacerItem(40, 20, QSizePolicy.Fixed, QSizePolicy.Minimum)
        self.btn_file = QPushButton("更新文函")
        self.btn_get_token = QPushButton("获取密钥") 

        lt_header.addWidget(self.btn_open_contract)
        lt_header.addWidget(self.btn_update_contract)
        lt_header.addItem(left_spacer)
        lt_header.addWidget(self.btn_reset_contract_info)
        lt_header.addWidget(self.btn_save_contract_info)
        lt_header.addItem(end_spacer)
        lt_header.addWidget(self.btn_get_token)
        lt_header.addWidget(self.btn_file)

        # endregion

        # region 信息展示标签和表格
        self.lb_message = QLabel(text="尚未授权访AED")
        self.lb_message.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum)
        self.lb_message.setAutoFillBackground(False)
        self.lb_message.setWordWrap(True)
        lt_pg.addWidget(self.lb_message)
        self.tb_contract = Base_Table("contract", contract_col_model_list)
        lt_pg.addWidget(self.tb_contract)
        self.tb_contract.reset_data()

        # endregion

    def bind(self):
        self.btn_file.clicked.connect(self.update_file)
        self.btn_open_contract.clicked.connect(self.open_contract_excel)
        self.btn_update_contract.clicked.connect(self.update_contract)
        self.btn_get_token.clicked.connect(get_token)
        # self.ck_has_delivery.clicked.connect(self.cmb_change)

    def update_contract(self):
        update_tb_contract()
        get_public_contract_info()
        self.data.Df_Show_Contract = self.data.Df_Contract
        self.tb_contract.reset_data(self.data.Df_Contract)
        show_msg("合同已更新", self.lb_message)
        self.btn_update_contract.setEnabled(False)

    def open_contract_excel(self):
        open_path(Contract_Excel_path)
        self.btn_update_contract.setEnabled(True)


    def update_file(self):
        self.update_file_thread = Th_Update_file()
        self.update_file_thread.child_signal.connect(lambda txt: show_msg(txt, self.lb_message))
        self.update_file_thread.start()

    def show_dialog(self, value):
        QMessageBox.information(self, title="合同文函更新", text=value, button0=QMessageBox.Yes, button1=QMessageBox.No)
